#!/bin/sh

# Slackware build script for HandBrakeCLI

# This slackbuild maintained by Klaatu at klaatu@hackerpublicradio.org
# Modified by David Somero at SlackBuilds.org.

PRGNAM=HandBrake
VERSION=${VERSION:-0.9.4}
ARCH=${ARCH:-i686}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Has to be i686 or x86_64
if [ "$ARCH" != "x86_64" ]; then
 ARCH="i686"
fi

# We are doing this to prevent HandBrake from downloading these
# files. SBo does not allow scripts/source to download stuff.
DLOADS="a52dec-0.7.4.tar.gz faac-1.28.tar.gz faad2-2.7.tar.gz 
ffmpeg-r19067.tar.bz2 lame-3.98.tar.gz libdca-r81-strapped.tar.gz 
libdvdnav-svn1168.tar.gz libdvdread-svn1168.tar.gz libmkv-0.6.4.tar.bz2 
libogg-1.1.3.tar.gz libsamplerate-0.1.4.tar.gz libtheora-1.1.0.tar.bz2 
libvorbis-aotuv_b5.tar.gz mp4v2-trunk-r355.tar.bz2 mpeg2dec-0.5.1.tar.gz 
x264-r1347-5ddd61b.tar.gz"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
mkdir download
for file in $DLOADS ; do mv $CWD/$file download ; done
chown -R root:root .
chmod -R a-s,u+w,g-w+r,o-w+r .

./configure \
  --prefix=/usr\
  --disable-gtk \
  --arch=$ARCH

cd build
  make
  install -D -m0755 HandBrakeCLI $PKG/usr/bin/HandBrakeCLI
cd ..

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | \
    xargs strip --strip-unneeded 2> /dev/null || true
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS COPYING CREDITS NEWS THANKS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
